#include "virtualdeviceproperty.h"
#include "virtualdeviceexception.h"
using namespace xvd;
using namespace xmlpp;

std::map<std::string, VirtualDeviceProperty::eAccess> VirtualDeviceProperty::m_accessmap;
VirtualDeviceProperty::eAccess VirtualDeviceProperty::getAccessCode(const std::string& index)
{
    if (m_accessmap.size() == 0)
    {
        m_accessmap["read-only"] = VirtualDeviceProperty::ReadOnly;
        m_accessmap["read-write"] = VirtualDeviceProperty::ReadWrite;
    }

    if (m_accessmap.find(index) != m_accessmap.end()) return m_accessmap[index];

    throw VirtualDeviceException(VirtualDeviceException::SyntaxError, "INVALID ACCESS MODE: " + index);
}

VirtualDeviceProperty::VirtualDeviceProperty(const Node* node) :
    m_variable(NULL)
{
    const Element* element = dynamic_cast<const Element*> (node);
    m_name = element->get_attribute("name")->get_value();
    m_access = getAccessCode(element->get_attribute("access")->get_value());
    m_variable = new VirtualDeviceVariable(element->get_children("variable").front());
}

VirtualDeviceProperty::VirtualDeviceProperty(const VirtualDeviceProperty& right) :
    m_name(right.m_name),
    m_access(right.m_access),
    m_variable(NULL)
{
    if(right.m_variable)
    {
        m_variable = new VirtualDeviceVariable(*right.m_variable);
    }
}

VirtualDeviceProperty::~VirtualDeviceProperty()
{
    delete m_variable;
}

std::string VirtualDeviceProperty::toXml() const
{
    return "";
}

bool VirtualDeviceProperty::fromXml(const std::string& data)
{
    return true;
}

std::string& VirtualDeviceProperty::name()
{
    return m_name;
}

VirtualDeviceProperty::eAccess& VirtualDeviceProperty::access()
{
    return m_access;
}

VirtualDeviceVariable& VirtualDeviceProperty::variable()
{
    return *m_variable;
}

const std::string& VirtualDeviceProperty::name() const
{
    return m_name;
}

VirtualDeviceProperty::eAccess VirtualDeviceProperty::access() const
{
    return m_access;
}

const VirtualDeviceVariable& VirtualDeviceProperty::variable() const
{
    return *m_variable;
}

